<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
        <link rel="stylesheet" href="../css/orderset.css">
        <!-- 引入组件库 -->
        <script src="../js/vue.js"></script>
        <script src="../plugins/elementui/index.js"></script>
        <script type="text/javascript" src="../js/jquery.min.js"></script>
        <script src="../js/axios-0.18.0.js"></script>
    </head>
    <style>
        .usual{
        }
        .fulled{
            color: red;
        }
    </style>
    <body class="hold-transition">
    <div id="app">
        <div class="content-header">
            <h1>预约管理<small>预约设置</small></h1>
            <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>预约管理</el-breadcrumb-item>
                <el-breadcrumb-item>预约设置</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="app-container">
            <div class="box">
                <div class="box ordersetting">
                    <el-card class="box-card">
                        <div class="boxMain">
                            <el-button style="margin-bottom: 20px;margin-right: 20px" type="primary" @click="downloadTemplate()">模板下载</el-button>
                            <el-upload action="/orderSetting/upload.do"
                                       name="excelFile"
                                       :show-file-list="false"
                                       :on-success="handleSuccess"
                                       :before-upload="beforeUpload">
                                <el-button type="primary">上传文件</el-button>
                            </el-upload>
                        </div>
                        <div>
                            操作说明：请点击"模板下载"按钮获取模板文件，在模板文件中录入预约设置数据后点击"上传文件"按钮上传模板文件。
                        </div>
                    </el-card>
                    <div class="calendar">

                        <el-calendar ref="settingCalendar" v-model="calendarValue">
                            <template slot="dateCell" slot-scope="{date, data}">
                                    {{date.getMonth() + 1}}-{{date.getDate()}}

                                    <!-- 日期大于当前日期才有设置按钮 -->
                                    <i v-if="date > today" class="el-icon-edit" @click="handleOrderSet(date)"></i>
                                    <br>
                                    <template v-for="item in leftobj">
                                        <template v-if="item.date == date.getDate() && date.getMonth() == item.month">
                                            <template v-if="item.number > item.reservations">
                                                <div class="usual">
                                                    <p>可预约{{item.number}}人</p>
                                                    <p>已预约{{item.reservations}}人</p>
                                                </div>
                                            </template>
                                            <template v-else>
                                                <div class="fulled">
                                                    <p>约满</p>
                                                </div>
                                            </template>
                                        </template>
                                    </template>

                            </template>
                        </el-calendar>

                    </div>
                </div>
            </div>
        </div>
    </div>
    </body>
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script>
        new Vue({
            el: '#app',
            data:{
                today:new Date(),//当前日期
                leftobj: [],//用于装载页面显示的月份已经进行预约设置的数据
                calendarValue:'',//日历当前的value
            },
            mounted: function () {
                  this.initData(null);
            },
            methods: {
				//预约设置
                handleOrderSet(date){
                //    格式化日期
                    var dateStr = this.formatDate(date.getFullYear(), date.getMonth()+1, date.getDate());
                //    弹出预约设置框
                    this.$prompt('请输入可预约人数', '预约设置', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        inputPattern: /^[1-9]\d*$/,
                        inputErrorMessage: '请输入正整数'
                    }).then(({ value }) => {
                    //    此处表示点击 确定 要执行的代码
                    //    1.将参数拼接称一个json对象
                        var param = {
                            orderDate:dateStr,
                            number:value
                        };
                    //    2.发送ajax请求编辑预约设置数据
                        axios.post("/orderSetting/editNumberByDate.do",param).then((res)=>{
                        //    提示用户
                            this.$message({
                                message:res.data.message,
                                type:res.data.flag?'success':'error'
                            });
                        //    刷新页面
                            this.initData(date);
                        })
                    }).catch(() => {
                    //    此处为点击 取消 要执行的代码
                        this.$message({
                            type: 'info',
                            message: '取消输入'
                        });
                    });
                },
				initData: function (date) {
                    if(!date){
                        //获取日历对象
                        let settingCalendar = this.$refs.settingCalendar;
                        //获取当前时间
                        date = settingCalendar.date;
                    }

                    let formatDate = date.getFullYear() + "-" + (date.getMonth() + 1);

                    //发送请求，查询预约设置数据
                    axios.get("/orderSetting/findByMonth.do?dateStr="+formatDate).then((res)=>{
                        this.leftobj = res.data.data;
                    })
                    // this.leftobj = [
                    //     {"date":22,"month":8,"number":300,"reservations":300,},
                    //     {"date":23,"month":8,"number":300,"reservations":10,},
                    // ]
                },
                // 返回 类似 2016-01-02 格式的字符串
                formatDate: function (year, month, day) {
                    var y = year;
                    var m = month;
                    if (m < 10) m = "0" + m;
                    var d = day;
                    if (d < 10) d = "0" + d;
                    return y + "-" + m + "-" + d
                },				//上传之前进行文件格式校验
                beforeUpload(file){
                    const isXLS = file.type === 'application/vnd.ms-excel';
                    if(isXLS){
                        return true;
                    }
                    const isXLSX = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
                    if (isXLSX) {
                        return true;
                    }
                    this.$message.error('上传文件只能是xls或者xlsx格式!');
                    return false;
                },
                //下载模板文件
                downloadTemplate(){
                    window.location.href="../../template/ordersetting_template.xlsx";
                },
                //上传成功提示
                handleSuccess(response, file) {
                    if(response.flag){
                        this.$message({
                            message: response.message,
                            type: 'success'
                        });
                    }else{
                        this.$message.error(response.message);
                    }
                    console.log(response, file, fileList);
                },
                //初始化当前页要展示的日期
            },
            watch:{
                //当点击上月 下月 当前月触发
                calendarValue(newVal,oldVal){
                    this.initData(newVal);
                }
            }
        })
    </script>
</html>
